<?php
require MODULE_ROOT . '/helpers/functions.php';

global $_GPC, $_W;
$op = $_GPC['op'] ?: 'index';

$user = user();
$config = config();
$uniacid = uniacid();
$uid = uid();
$bid = input('bid', 0);
$group = $user['group'] ? get_group_info_by_id($user['group']) : false;

check_auth();

if($op == 'index') {
    $filter = ['bid' => $bid, 'op' => 'list'];

    $bankSql = 'SELECT * FROM ' . tablename('d1sj_card_bank') . 'WHERE uniacid = :uniacid';
    $banks = pdo_fetchall($bankSql, [':uniacid' => $uniacid]);
    $tmp = [];
    foreach ($banks as $bank) {
        $tmp[$bank['id']] = $bank;
    }
    $banks = $tmp;
    include $this->template('card');
} else if($op == 'list') {
    $where = [
        "uniacid = {$uniacid}",
    ];
    if($tmpInput = input('bid'))
        array_push($where, "bank = {$tmpInput}");
    $count = 'SELECT count(id) FROM ' . tablename('d1sj_card_card') . ' WHERE ' . implode(' AND ', $where);
    $total = get_page_total($count);

    $sql = 'SELECT `photo`,`title`,`desc`,`id` FROM ' . tablename('d1sj_card_card') . ' WHERE ' . implode(' AND ', $where) . ' ' . get_limit();
    $lists = pdo_fetchall($sql);
    foreach ($lists as $key => $list) {
        $lists[$key]['url'] = $this->createMobileUrl('card', ['op' => 'apply', 'cid' => $list['id']]);
        unset($lists[$key]['id']);
    }
    return_json([
        'result' => $lists,
        'total'  => $total,
        'code'   => count($lists) ? 1 : 0,
        'reason' => count($lists) ? '' : '没有更多了'
    ]);

} else if($op == 'apply') {
    $cid = input('cid');
    $bankSql = 'SELECT * FROM ' . tablename('d1sj_card_bank') . 'WHERE uniacid = :uniacid';
    $banks = pdo_fetchall($bankSql, [':uniacid' => $uniacid]);
    $tmp = [];
    foreach ($banks as $bank) {
        $tmp[$bank['id']] = $bank;
    }
    $banks = $tmp;
    $cardSql = 'SELECT * FROM ' . tablename('d1sj_card_card') . ' WHERE id = :id AND uniacid = :uniacid';
    $card = pdo_fetch($cardSql, [':id' => $cid, ':uniacid' => $uniacid]);
    include $this->template('card');
} else if($op == 'submit') {
    #判断用户是否填写资料
    $user_sql = 'SELECT * FROM ' . tablename('d1sj_card_user') . ' WHERE uid = :uid';
    $user_list = pdo_fetch($user_sql, [':uid' => $uid]);
    if(!$user_list['mobile']){
         message('请先填写资料', $this->createMobileUrl('user'), 'error');
    }
    $cid = input('id');
    $cardSql = 'SELECT * FROM ' . tablename('d1sj_card_card') . ' WHERE id = :id AND uniacid = :uniacid';
    $card = pdo_fetch($cardSql, [':id' => $cid, ':uniacid' => $uniacid]);

    $sql = 'SELECT * FROM ' . tablename('d1sj_card_bank') . ' WHERE id = :id';
    $bank = pdo_fetch($sql, [':id' => $card['bank']]);

    $input = input('data');
    $name = $_GPC['data']['name'];
    $mobile = $_GPC['data']['mobile'];
    $data = [
        'uniacid'    => $uniacid,
        'uid'        => $uid,
        'bank'       => $cid,
        'name'       => $input['name'],
        'mobile'     => $input['mobile'],
        'createtime' => time(),
    ];
    $rates = explode('|', $config['rate']);
    $bankMoney = $bank['price'];
    $data['fan_total'] = $bankMoney;
    if($user['leader1']) {
        $group1 = get_group_info_by_id($user['leader1'], true);
        $groupLevel1 = $group1 ? $group1['mlevel'] : 0;
    }
    if($user['leader2']) {
        $group2 = get_group_info_by_id($user['leader2'], true);
        $groupLevel2 = $group2 ? $group2['mlevel'] : 0;
    }
    if($user['leader3']) {
        $group3 = get_group_info_by_id($user['leader3'], true);
        $groupLevel3 = $group3 ? $group3['mlevel'] : 0;
    }

    if($user['leader1'] && $rates[0] && $groupLevel1 >= 1) {
        $data['fan_uid1'] = $user['leader1'];
        $data['fan_level1'] = $bankMoney * ($rates[0] / 100);
    }
    if($user['leader2'] && $rates[1] && $groupLevel2 >= 2) {
        $data['fan_uid2'] = $user['leader2'];
        $data['fan_level2'] = $bankMoney * ($rates[1] / 100);
    }
    if($user['leader3'] && $rates[2] && $groupLevel3 >= 3) {
        $data['fan_uid3'] = $user['leader3'];
        $data['fan_level3'] = $bankMoney * ($rates[2] / 100);
    }
    $res = pdo_insert('d1sj_card_record', $data);

    // 增加银行卡申请数
    pdo_update('d1sj_card_bank', [
        'num' => $bank['num'] + 1
    ], [
        'id' => $bank['id']
    ]);
    header('Location: ' . $card['api']);
}
